<?php

    /**
     * Url
     *
     * A specific selector for the xml files which contains the configuration of the engine
     *
     * @package      Core
     * @subpackage   Selector
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Selector_Url extends Core_Selector_Abstract
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * The file specified in the selector
         */
        private $_file = '';

        /**
         * Path to the cache file
         *
         * @var string
         */
        private $_cachePath = '';

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns the class name
         *
         * @return      string
         */
	    public function getClass()
		{
	        return $this->_module . '_Classes_UrlHandlers_'. $this->_file;
	    }

		/**
         * Returns the path to the cache file
         *
         * @return      string
         */
        public function getCachePath()
		{
			return $this->_cachePath;
		}

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
		 * @param       string      $selector
		 * @throws		Core_Exception
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
         */
        public function __construct($selector)
        {
            if ($selector === null) {
				throw new Core_ArgumentNullException('selector is a null reference');
			}

            if (strlen(trim($selector)) == 0) {
				throw new Core_ArgumentException('selector is a zero-length string');
			}

            $this->_registry = Zend_Registry::getInstance();

            if (preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_\.\\/]+)$/", $selector, $m)) {
                if ($m[1] != '' && $m[2] != '') {
                    $this->_module = $m[2];
                } else {
                    $this->_module = Core_Context::get();
                }

                $this->_file = $m[3];
                $this->_resource = $m[3];

                $this->_createPath();
                $this->_createCachePath();
            } else {
                throw new Core_Exception('Invalid url selector syntax : '. $selector);
            }
        }

    ////////////////////////////////////////////////////////////////////////////
	//                            Protected methods                           //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * Sets the real path to the defined file
         *
         * @return      void
         */
        protected function _createPath()
        {
			$path = Core_IO_Path::combine(
                                            PROJECT_MODULE_PATH,
                                            ucfirst(strtolower($this->_module)),
                                            'Classes',
                                            'UrlHandlers',
                                            $this->_file . '.php'
                                        );

            if (!Core_IO_File::exists($path)) {
                $path = Core_IO_Path::combine(
                                                SERUM_MODULE_PATH,
                                                ucfirst(strtolower($this->_module)),
                                                'Classes',
                                                'UrlHandlers',
                                                $this->_file . '.php'
                                            );
            }

            $this->_path = $path;
        }

	    /**
         * Sets the path to the cache file
         *
         * @return      void
         */
        protected function _createCachePath()
		{
			$this->_cachePath = Core_IO_Path::combine(
                                                      PROJECT_TEMP_URLS,
                                                      '~'
                                                      . strtolower($this->_module)
                                                      . '~'
                                );
	    }
    }
